import { createApp } from "vue";
import pinia from "./store";
import { setupAssets, setupWechatShare } from "./plugins";
import "./assets/font/iconfont.css";
import { Input, Picker, Textarea, Button, Tabs, Searchbar,
  Overlay, Divider, TabPane, Popup, DatePicker, Form, FormItem, Calendar, Radio, RadioGroup, Swiper, SwiperItem } from "@nutui/nutui-taro";
import { get, post } from "./utils/request";
import { Sticky } from "@nutui/nutui-taro";

// import { IconFont } from '@nutui/icons-vue-taro'

import { useUserStore } from "./store/user";
import Taro from "@tarojs/taro";
import { wechatAuth } from "./core/config";
import LocationPermissionGuideOverlay from "@/components/LocationPermissionGuideOverlay.vue";

// post('/dictionary/region', {
//   fatherId: 7459
// }).then(res => {
//   console.log("res", res);
// });

/**
 * 检查微信授权状态
 */
// function checkWechatAuth() {
//   try {
//     // 检查是否需要授权
//     if (wechatAuth.shouldAuth()) {
//       console.log("需要微信授权，开始授权流程");
//       wechatAuth.authorize();
//       return;
//     }

//     // 检查授权状态
//     if (wechatAuth.checkAuthStatus()) {
//       console.log("授权状态正常，获取用户信息");
//       const userStore = useUserStore();
//       userStore.fetchUserInfo();
//     } else {
//       console.log("授权状态异常，跳转到注册页面");
//       Taro.navigateTo({
//         url: "/pages/webs/register",
//       });
//     }
//   } catch (error) {
//     console.error("检查微信授权失败:", error);
//     // 出错时跳转到注册页面
//     Taro.navigateTo({
//       url: "/pages/webs/register",
//     });
//   }
// }

const App = createApp({
  onShow() {
    // 应用启动时恢复用户信息
    const userStore = useUserStore();
    userStore.restoreUserInfo();
    setTimeout(async () => {
      await userStore.fetchUserInfo();
    }, 900);

    //检查页面是否为回调页面
    wechatAuth.handleAuthCallback();
    // 检查微信授权状态
    // checkWechatAuth();
  },
  /** 入口组件不需要实现 render 方法，即使实现了也会被 taro 所覆盖 */
  onLaunch() {
    // Taro.showTabBarRedDot({
    //   index: 3
    // })
    setInterval(() => {
      // post /message/number
      if (Taro.getStorageSync("token")) {
        post("/message/number").then((res) => {
          // console.log("res", res);
          if (res.data) {
            Taro.showTabBarRedDot({
              index: 3,
            });
          }
        });
      }
    }, 8000);
  },
});

// 引入静态资源和样式
setupAssets();
setupWechatShare();

// 注册 Pinia 状态管理
App.use(pinia);
App.use(Input);
App.use(Picker);
App.use(Textarea);
App.use(Button);
App.use(Tabs);
App.use(TabPane);
App.use(Popup);
App.use(DatePicker);
App.use(Form);
App.use(FormItem);
App.use(Calendar);
App.use(Radio);
App.use(RadioGroup);
App.use(Sticky);
App.use(Divider);
App.use(Swiper);
App.use(SwiperItem);
App.use(Overlay);
App.use(Searchbar);

// App.use(IconFont)

// if (process.env.TARO_ENV === "h5" && typeof document !== "undefined") {
//   const guideContainer = document.createElement("div");
//   guideContainer.id = "location-permission-guide-root";
//   document.body.appendChild(guideContainer);
//   createApp(LocationPermissionGuideOverlay).mount(guideContainer);
// }

export default App;
